package com.medical.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.medical.core.result.Result;
import com.medical.satoken.utils.LoginHelper;
import com.medical.system.model.form.MenuForm;
import com.medical.system.model.query.MenuQuery;
import com.medical.system.model.vo.MenuOption;
import com.medical.system.model.vo.MenuVO;
import com.medical.system.model.vo.RouteVO;
import com.medical.system.service.SysMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "03.菜单接口")
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/menu")
@Slf4j
public class SysMenuController {

    private final SysMenuService menuService;

    @Operation(summary = "获取菜单列表")
    @GetMapping("/list")
    public Result<List<MenuVO>> getMenuList(@ParameterObject MenuQuery query) {
        Long userId = LoginHelper.getUserId();
        return Result.success(menuService.getMenuList(query, userId));
    }

    @Operation(summary = "获取资源树")
    @GetMapping("/tree")
    public Result<List<MenuOption>> getResourceTree() {
        return Result.success(menuService.getResoutceList());
    }

    @Operation(summary = "获取菜单下拉列表")
    @GetMapping("/options")
    public Result<List<MenuOption>> getMenuOptions() {
        return Result.success(menuService.getMenuOptions());
    }

    @Operation(summary = "路由列表")
    @GetMapping("/routes")
    public Result<List<RouteVO>> getRouteList() {
        return Result.success(menuService.getRouteList(LoginHelper.getUserId()));
    }

    @Operation(summary = "获取菜单表单数据")
    @GetMapping("/{menuId}/form")
    public Result<MenuForm> getMenuForm(@Parameter(description = "菜单ID") @PathVariable(required = true) Long menuId) {
        return Result.success(menuService.getMenuForm(menuId));
    }

    @Operation(summary = "新增菜单")
    @PostMapping
    @CacheEvict(cacheNames = "menu", key = "'routes'")
    @SaCheckPermission("sys:menu:add")
    public Result saveMenu(@Valid @RequestBody MenuForm menuForm) {
        return Result.judge(menuService.saveMenu(menuForm));
    }

    @Operation(summary = "修改菜单")
    @PutMapping("/{id}")
    @CacheEvict(cacheNames = "menu", key = "'routes'")
    @SaCheckPermission("sys:menu:edit")
    public Result updateMenu(@Valid @RequestBody  MenuForm menuForm) {
        return Result.judge(menuService.saveMenu(menuForm));
    }

    @Operation(summary = "删除菜单")
    @DeleteMapping("/{id}")
    @CacheEvict(cacheNames = "menu", key = "'routes'")
    @SaCheckPermission("sys:menu:delete")
    public Result deleteMenu(@Parameter(description = "菜单ID") @PathVariable Long id) {
        return Result.success(menuService.deleteMenu(id));
    }

    @Operation(summary = "修改菜单显示状态")
    @PatchMapping("/{menuId}")
    @SaCheckPermission("sys:menu:update_visible")
    public Result updateMenuVisible(@Parameter(description = "菜单ID") @PathVariable  Long menuId,
                                    @Parameter(description = "显示状态（1：显示、0：隐藏）") @RequestParam Integer visible) {
        return Result.success(menuService.updateMenuVisible(menuId, visible));
    }
}
